var operadataObj = {
    dStartDate: "",
    dEndDate: "",
    nPageIndex: "0",
    strCondition: "",
    flage: 0,
    init: function () {
        publicObj.getIosAndData();
        this.selectTime();
        this.makeODParametter();
        this.loadMore();
        this.serach();
    },
    makeODParametter: function () {
        var odpObj = {};
        //查询当天时间
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        if(month < 10){
            month = "0"+month;
        }
        if(day < 10){
            day = "0" + day;
        }
        var todayStr = year + '-' + month + '-' + day;
        //请求参数
        $("#beginTime").val(todayStr);
        $("#endTime").val(todayStr);
        this.dStartDate = todayStr;
        this.dEndDate = todayStr;
        this.nPageIndex = "0";
        this.getOperaData()
    },
    //选择时间段
    selectTime: function () {
        $('#beginTime').date({theme: "date"}, this.changeBeginTime, this.tese, this);
        $('#endTime').date({theme: "date"}, this.changEndTime, this.tese, this);
    },
    changeBeginTime: function (datestr, obj) {
        obj.flage = 0;
        obj.nPageIndex = "0";
        var beginTime = $('#beginTime').val(datestr);
        console.log(datestr)
        obj.dStartDate = datestr;
        obj.changeodObj();
    },
    changEndTime: function (datestr, obj) {
        obj.flage = 0;
        obj.nPageIndex = "0";
        var endTime = $('#endTime').val(datestr);
        obj.dEndDate = datestr;
        obj.changeodObj();
    },
    changeodObj: function () {
        var bTime = new Date(this.dStartDate).getTime();
        var eTime = new Date(this.dEndDate).getTime();
        var date = (new Date()).getTime();
        if (bTime > eTime) {
            publicObj.myalert('开始时间不能大于结束时间')
        } else {
            if (eTime > date) {
                publicObj.myalert('结束时间不能大于当前时间')
            } else {
                this.getOperaData();
            }
        }

    },
    tese: function () {
    },
    loadMore: function () {
        var me = this;
        $(window).scroll(function () {
            if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
                if (me.flage == 0) {

                    me.nPageIndex  = (Number(me.nPageIndex)+1).toString();
                    me.getOperaData(2);
                } else {
                    publicObj.myalert("没有更多啦~~")
                }

            }
        });
    },
    serach: function () {
        var me = this;
        $(".serachbtn").bind("click", function () {
            var searchVal = $(".search").val();
            console.log(searchVal)
            if (searchVal == "") {
                me.nPageIndex= "0";
                me.flage= 0;
                me.strCondition = searchVal;
                me.getOperaData()
            } else {
                me.nPageIndex = "0";
                me.strCondition = searchVal;
                console.log(me.odObj)
                me.getOperaData(1)

            }
        })
    },
    getOperaData: function (callbackparameter) {
        var reqType = "POST";
        var isAsync = true;
        var dataType = "json";
        var contentType = 'application/x-www-form-urlencoded';
        var reqUrl = ruquireObj.urlPrefix + 'OrderService/getDeptMallOrders';
        var a = {};
        a.dStartDate = this.dStartDate;
        a.dEndDate = this.dEndDate;
        a.nPageIndex = this.nPageIndex;
        console.log(this.strCondition)
        if (this.strCondition != "") {
            a.strCondition = this.strCondition;
        }
        var callbackparameter = callbackparameter;

        publicObj.myAjax(this, this.getOperaDataSug, reqUrl, a, reqType, isAsync, dataType, contentType, callbackparameter);
        publicObj.showLoad();
    },
    getOperaDataSug: function (res, obj, callbackparameter) {
        publicObj.hideLoad();
        console.log(callbackparameter)
        var me = obj;
        if (res.return.nCode == 0) {
            var resObj = res.response;
            console.log(resObj);
            //经营数据总额
            if(callbackparameter != 1){
                var orderCountData = resObj.cdoOrderCountData;
                $(".allunm").text(orderCountData.nOrderCount);
                if (orderCountData.nOrderCount == 0) {
                    $(".money").text(orderCountData.nOrderCount);
                } else {
                    $(".money").text(orderCountData.nSumAmount);
                }
            }

            //数据列表
            var dataList = resObj.cdoDataList;
            console.log(dataList);
            if (dataList.length == 0) {
                me.flage = 1;
            }
            //隐藏经营数据
            if(callbackparameter != 2 && dataList.length == 0){
                $(".opreadatabox").hide();
            }else {
                $(".opreadatabox").show();
            }
            var str = '';
            for (var i = 0; i < dataList.length; i++) {
                //商品信息列表
                var goodsStr = '';
                for (var j = 0; j < dataList[i].posSaledtl.length; j++) {
                    goodsStr += '<div class="center kind1"><div class="goodsname"><div class="goodsnametxt">' + dataList[i].posSaledtl[j].sGoodsName + '</div><div class="barcode">' + dataList[i].posSaledtl[j].sBarcode + '</div></div><div class="pric">￥' + publicObj.reduceNum(dataList[i].posSaledtl[j].nPluAmount,dataList[i].posSaledtl[j].nPluDis) + '</div><div class="num">' + dataList[i].posSaledtl[j].nQty + '</div></div>'
                }
                //商品图片列表
                var imgStr = "";
                if (dataList[i].posHeadExt) {
                    var imgArry = dataList[i].posHeadExt.sImgUrl.split(",");
                    if(imgArry[0] != ""){
                        for (var k = 0; k < imgArry.length; k++) {
                            imgStr += ' <div class="imgbox"><img src="' + imgArry[k] + '"></div>';
                        }
                    }else {
                        imgStr="";
                    }

                }
                str += '<div class="datalistbox"><div class="top">收款时间' + dataList[i].dCreateTime + '</div><div class="centerbox"><div class="normalgoods">' + goodsStr + '</div><div class="center kind2">' + imgStr + '</div><div class="totalbox totalbox2"><div class="total"> 共' + dataList[i].nTotalQty + '件商品，实收<text>￥' + dataList[i].nRealPayVal + '</text></div></div></div><div class="bottom"><div orderid = "' + dataList[i].sDealCount + '" class="seeorder">查看详情</div></div></div>';
            }
            if (callbackparameter == 2) {
                $('.datalistwall').append(str);
            } else {
                if(dataList.length == 0){
                    $(".datalistwall").hide();
                    $(".no").show();
                }else {
                    $(".datalistwall").show();
                    $(".no").hide();
                    $('.datalistwall').html(str);
                }


            }



            me.toOrder();
        } else {
            $(".datalistwall").hide();
            $(".opreadatabox").hide();
            $(".selectTimebox").hide();
            $(".sarchbox").hide();
            $(".no").show();
        }
    },

    //前往订单详情
    toOrder: function () {
        $(".seeorder").each(function () {
            $(this).unbind('click').bind('click', function (e) {
                var orderid = $(this).attr("orderid")
                location.href = "./transaction.html?oriderid=" + orderid + '&form=operadata';

            })
        });
    }
};
operadataObj.init();